from config import dbCon
class permission:
    def __init__(self, permId=0, permName=None, permStaut=None):
        self.permId = permId
        self.permName = permName
        self.permStatus = permStatus
    def addPerm(self, permName, permStatus=1):
        if permName:
            count=dbCon.insert('permission',
                         permission_name=permName,
                         permission_status=permStatus)
            return count
    def getPerm(self, roleId=None):
        permList=[]
        if roleId:
            result = dbCon.select('role t1 join role_permission_link t2 on t1.role_id=t2.role_id join permission t3 on t2.permission_id=t3.permission_id',
                                  where('t1.role_id=$v_role_id'),
                                  vars=dict(v_role_id=roleId))
        else:
            result = dbCon.select('permission')
        for x in result:
            perm = permission()
            perm.permId=perm['permission_id']
            perm.permName=perm['permission_name']
            perm.permStatus=perm['permission_status']
            permList.append(perm);
        return permList
    def updatePerm(self, permId, permName=None, permStatus=None):
        if permId and (permName or permStatus):
            count = dbCon.update('permission',
                         perm_name=permName,
                         perm_status=permStatus,
                         where=('perm_id=$v_perm_id'),
                         vars=dict(v_perm_id=permId))
        return count
    